﻿namespace SERFLITEMAX20.DataObjects
{
    using System;
    using System.Collections;
    using System.Diagnostics;
    using System.IO;
    using System.Text;
    using System.Xml;
    using System.Xml.Serialization;

    [XmlRoot("areas", Namespace="", IsNullable=false)]
    public class Areas : IDisposable
    {
        [XmlElement("area", typeof(Area))]
        public ArrayList AreaItemsArray = new ArrayList();

        public void Add(int pid, int pmapdid, int pareatableid, string pname)
        {
            Area area = new Area();
            area.id = pid;
            area.mapid = pmapdid;
            area.areatableid = pareatableid;
            area.name = pname;
            this.AreaItemsArray.Add(area);
        }

        public static Areas Load()
        {
            Areas areas = new Areas();
            if (File.Exists("areas.xml"))
            {
                try
                {
                    XmlTextReader xmlReader = new XmlTextReader("areas.xml");
                    XmlSerializer serializer = new XmlSerializer(areas.GetType());
                    areas = (Areas) serializer.Deserialize(xmlReader);
                    xmlReader.Close();
                }
                catch (Exception exception)
                {
                    Debug.WriteLine(exception.Message);
                    areas = new Areas();
                }
            }
            return areas;
        }

        public void Save()
        {
            XmlTextWriter writer = new XmlTextWriter("areas.xml", Encoding.ASCII);
            new XmlSerializer(base.GetType()).Serialize((XmlWriter) writer, this);
            writer.Close();
        }

        void IDisposable.Dispose()
        {
            this.AreaItemsArray.Clear();
            this.AreaItemsArray = null;
        }
    }
}

